package com.ayq.activiti.system.controller;

import com.ayq.activiti.common.util.Constant;
import com.ayq.activiti.common.util.DataResult;
import com.ayq.activiti.system.common.utils.RedisKit;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 验证码相关
 */
@Api(tags = "验证码相关")
@RestController
@Slf4j
@RequestMapping("/sys")
public class KaptchaController {

    @Autowired
    private RedisKit<Object> redisKit;

    /**
     * 获取验证码图片
     * Gets captcha code.
     *
     * @param request  the request
     * @param response the response
     * @throws IOException the io exception
     */
    @GetMapping("/getVerify")
    public DataResult getCaptchaCode(@RequestParam("randomx") String randomx, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
        captcha.setLen(2);
        redisKit.set(Constant.IMG_TOKEN + randomx, captcha.text(), 60);
        return DataResult.success(captcha.toBase64());
    }

}
